<template>
  <div>
    <cus-modal
      ref="modal"
      width="400"
      :title="isAdmin == 1 ? '账号交接' : '重置密码'"
      @on-visible-change="onVisibleChange"
      @onOk="onOk"
    >
      <Form ref="formValidate" :model="form" :rules="ruleValidate" :label-width="100">
        <FormItem
          label="账号交接"
          prop="loginName"
          v-if="isAdmin == 1"
          :rules="{ required: true, message: '交接账号不能为空', trigger: 'blur' }"
        >
          <cus-autoComplete
            :value="form"
            key-label="loginName"
            key-value="id"
            placeholder="请输入账号名称选择账号"
            return-label="data"
            @onChange="changeUserOption"
            :cusParams="{ unitCode: vuex_user.base.unitCode, status: 1 }"
            fncLabel="loginName"
            pageSize="size"
            currentPage="current"
            :fnc="getUserListApi"
          ></cus-autoComplete>
        </FormItem>
        <FormItem label="新密码" prop="password" v-if="isAdmin == 0">
          <Input
            v-model.trim="form.password"
            type="password"
            password
            style="width: 200px"
            placeholder="请输入密码"
          ></Input>
        </FormItem>
        <FormItem
          label="确认密码"
          prop="passwordNotarize"
          v-if="isAdmin == 0"
        >
          <Input
            v-model.trim="form.passwordNotarize"
            type="password"
            password
            style="width: 200px"
            placeholder="请确认密码"
          ></Input>
        </FormItem>
      </Form>
      <Spin size="large" fix v-if="loading"></Spin>
    </cus-modal>
  </div>
</template>

<script>
import { getUserList, handoverUser, resetUserPassword } from "@/api/business/system";


export default {
  name: "handleModal",
  data() {

    return {
      loading: false,
      form: {
  id: "",
  name: "",
  loginName: "",
  phoneNumber: "",
  postName: "", //职务
  unitName: "", //单位
  password: "",
  passwordNotarize: "", //确认密码
  descr: "",
  roleNameList: [], //角色名称
  roleIdList: [], //角色id
},
      isAdmin: "", //是否为交接
      userList: [],
      roleList: [],
      ruleValidate: {
        password: [
          {
            required: true,
            min: 5,
            max: 32,
            message: "密码长度为5-32位",
            trigger: "blur",
          },
          {
          
            validator: (rule, value, callback) => {
              let reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
              if (reg.test(value)) {
                callback(new Error("密码不能含有中文"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        passwordNotarize: [
        
          {
            required: true,
            validator: this.passwordVerifyValidate,
            trigger: "change",
          },
        ],
      },
      getUserListApi: getUserList,
      pages: {
        page: 1,
        pageSize: 20,
        pageTotal: 0,
      },
    };
  },
  mounted() {
  
  },
  methods: {
    onVisibleChange(v) {
      this.$refs["formValidate"].resetFields();
    },
    show() {
      this.$refs["modal"].show = true;
      console.log(this.form.id);
    },
      passwordVerifyValidate (rule, value, callback)  {


        if (this.form.password == "") {
        callback();
      } else if (value == "") {
        callback(new Error("请确认密码"));
      } else if (value != this.form.password) {
        callback(new Error("密码不一致"));
      } else {
        callback();
      }
 
    },
    onOk() {
      this.$refs["formValidate"].validate((valid) => {
        if (valid) {
          let contentA = "确认重置密码";
          let contentB = "确认将当前帐号交接给" + this.form.loginName;
          this.$cu
            .dialog({
              content: this.isAdmin == 0 ? contentA : contentB,
            })
            .then(() => {
              if (this.isAdmin == 0) {
     
                  this.getResetUserPassword();
                
              } else {
                this.getHandoverUser();
              }
            });
        }
      });
    },
    // 改变用户
    changeUserOption(val) {
      console.log(111111111111111111111);
      this.$refs["formValidate"].resetFields();
      this.form.id = val.id;
      this.form.loginName = val.loginName;
    },

    // 重置密码
    getResetUserPassword() {
      console.log("用户id", this.form.id);
      this.form.loading = true;
      return new Promise((resolve) => {
        resetUserPassword({
          userId: this.form.id,
          password: this.form.password,
        }).then((res) => {
          if (res.code === 1) {
            this.form.loading = false;
            this.$Message.success("重置密码成功");
            this.$refs["modal"].show = false;
            this.$parent.onInit();
            resolve();
          }
        });
      });
    },
    //   管理员交接
    getHandoverUser() {
      this.form.loading = true;
      return new Promise((resolve) => {
        handoverUser({
          toUserId: this.form.id,
        }).then((res) => {
          if (res.code === 1) {
            this.form.loading = false;
            this.$Message.success("交接成功");
            this.$refs["modal"].show = false;
            this.$parent.onInit();
            resolve();
          }
        });
      });
    },
  },
};
</script>

<style scoped lang="less">
.permission {
  height: 300px;
  width: 500px;
  border: 1px solid #dcdee2;
  overflow-y: auto;
  border-radius: 2px;
  padding: 0 0 0 10px;
}
</style>
